<script setup lang="ts">
import { ref, computed } from 'vue'
import { User, Setting, Bell, Lock, Delete } from '@element-plus/icons-vue'
import ThemeSwitcher from '@/components/ThemeSwitcher.vue'

interface UserProfile {
  id: number
  username: string
  avatar: string
  level: number
  experience: number
  joinDate: string
  lastActive: string
  achievements: number
  goals: number
  diaries: number
  theme: string
  notifications: boolean
  privacy: 'public' | 'private' | 'friends'
}

const profile = ref<UserProfile>({
  id: 1,
  username: '时空探索者',
  avatar: '👨‍🚀',
  level: 15,
  experience: 750,
  joinDate: '2024-01-01',
  lastActive: '2024-03-20 14:30',
  achievements: 12,
  goals: 5,
  diaries: 28,
  theme: '量子深空',
  notifications: true,
  privacy: 'public',
})

const activeTab = ref('profile')
const tabs = [
  { label: '个人信息', value: 'profile', icon: User },
  { label: '账号设置', value: 'settings', icon: Setting },
  { label: '通知管理', value: 'notifications', icon: Bell },
  { label: '隐私设置', value: 'privacy', icon: Lock },
]

const experiencePercentage = computed(() => {
  return (profile.value.experience / 1000) * 100
})

const getLevelColor = (level: number) => {
  if (level >= 20) return 'var(--supernova-orange)'
  if (level >= 10) return 'var(--laser-cyan)'
  return 'var(--hologram-purple)'
}
</script>

<template>
  <div class="profile-container">
    <el-card class="header-card">
      <div class="header-content">
        <div class="profile-info">
          <div class="avatar-section">
            <div class="avatar">{{ profile.avatar }}</div>
            <div class="level-badge" :style="{ backgroundColor: getLevelColor(profile.level) }">
              Lv.{{ profile.level }}
            </div>
          </div>
          <div class="info-section">
            <h2>{{ profile.username }}</h2>
            <div class="experience-bar">
              <el-progress
                :percentage="experiencePercentage"
                :color="getLevelColor(profile.level)"
                :show-text="false"
                :stroke-width="4"
              />
              <div class="experience-text">{{ profile.experience }}/1000 经验值</div>
            </div>
          </div>
        </div>
        <div class="stats-grid">
          <div class="stat-item">
            <div class="stat-value">{{ profile.achievements }}</div>
            <div class="stat-label">成就数</div>
          </div>
          <div class="stat-item">
            <div class="stat-value">{{ profile.goals }}</div>
            <div class="stat-label">目标数</div>
          </div>
          <div class="stat-item">
            <div class="stat-value">{{ profile.diaries }}</div>
            <div class="stat-label">日记数</div>
          </div>
        </div>
      </div>
    </el-card>

    <el-card class="content-card">
      <template #header>
        <div class="card-header">
          <el-tabs v-model="activeTab" class="content-tabs">
            <el-tab-pane v-for="tab in tabs" :key="tab.value" :name="tab.value">
              <template #label>
                <el-icon><component :is="tab.icon" /></el-icon>
                <span>{{ tab.label }}</span>
              </template>
            </el-tab-pane>
          </el-tabs>
        </div>
      </template>

      <div v-if="activeTab === 'profile'" class="profile-section">
        <div class="info-grid">
          <div class="info-item">
            <div class="info-label">加入时间</div>
            <div class="info-value">{{ profile.joinDate }}</div>
          </div>
          <div class="info-item">
            <div class="info-label">最后活跃</div>
            <div class="info-value">{{ profile.lastActive }}</div>
          </div>
          <div class="info-item">
            <div class="info-label">当前主题</div>
            <div class="info-value">{{ profile.theme }}</div>
          </div>
        </div>
      </div>

      <div v-else-if="activeTab === 'settings'" class="settings-section">
        <el-form label-position="top" class="settings-form">
          <el-form-item label="用户名">
            <el-input v-model="profile.username" />
          </el-form-item>
          <el-form-item label="头像">
            <el-select v-model="profile.avatar" class="avatar-select">
              <el-option label="👨‍🚀 时空探索者" value="👨‍🚀" />
              <el-option label="👩‍🚀 星际旅人" value="👩‍🚀" />
              <el-option label="🤖 量子机器人" value="🤖" />
              <el-option label="👾 外星访客" value="👾" />
            </el-select>
          </el-form-item>
          <el-form-item label="主题">
            <ThemeSwitcher />
          </el-form-item>
          <el-form-item>
            <el-button type="primary" class="save-button">保存更改</el-button>
          </el-form-item>
        </el-form>
      </div>

      <div v-else-if="activeTab === 'notifications'" class="notifications-section">
        <el-form label-position="top" class="notifications-form">
          <el-form-item label="通知设置">
            <el-switch
              v-model="profile.notifications"
              active-text="开启通知"
              inactive-text="关闭通知"
            />
          </el-form-item>
          <el-form-item label="通知类型">
            <el-checkbox-group>
              <el-checkbox label="achievement">成就解锁</el-checkbox>
              <el-checkbox label="goal">目标完成</el-checkbox>
              <el-checkbox label="diary">日记提醒</el-checkbox>
              <el-checkbox label="system">系统通知</el-checkbox>
            </el-checkbox-group>
          </el-form-item>
        </el-form>
      </div>

      <div v-else class="privacy-section">
        <el-form label-position="top" class="privacy-form">
          <el-form-item label="隐私设置">
            <el-radio-group v-model="profile.privacy">
              <el-radio label="public">公开</el-radio>
              <el-radio label="friends">仅好友可见</el-radio>
              <el-radio label="private">私密</el-radio>
            </el-radio-group>
          </el-form-item>
          <el-form-item label="数据管理">
            <el-button type="danger" :icon="Delete">删除账号</el-button>
          </el-form-item>
        </el-form>
      </div>
    </el-card>
  </div>
</template>

<style scoped>
.profile-container {
  padding: 20px;
}

.header-card {
  margin-bottom: 20px;
  background: var(--bg-card);
  border: 1px solid var(--border-color);
  backdrop-filter: blur(10px);
}

.header-content {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.profile-info {
  display: flex;
  align-items: center;
  gap: 20px;
}

.avatar-section {
  position: relative;
}

.avatar {
  width: 80px;
  height: 80px;
  font-size: 40px;
  display: flex;
  align-items: center;
  justify-content: center;
  background: var(--bg-secondary);
  border-radius: 50%;
  border: 2px solid var(--border-color);
  transition: all 0.3s ease;

  &:hover {
    transform: scale(1.1);
    border-color: var(--laser-cyan);
    box-shadow: 0 0 20px rgba(0, 255, 209, 0.3);
  }
}

.level-badge {
  position: absolute;
  bottom: -5px;
  right: -5px;
  padding: 2px 8px;
  border-radius: 10px;
  font-size: 12px;
  color: white;
  font-weight: bold;
}

.info-section {
  h2 {
    margin: 0 0 10px;
    font-size: 24px;
    color: var(--text-primary);
  }
}

.experience-bar {
  width: 200px;
}

.experience-text {
  font-size: 12px;
  color: var(--text-secondary);
  margin-top: 5px;
}

.stats-grid {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 30px;
}

.stat-item {
  text-align: center;
}

.stat-value {
  font-size: 24px;
  font-weight: bold;
  color: var(--laser-cyan);
  margin-bottom: 5px;
  text-shadow: 0 0 10px rgba(0, 255, 209, 0.5);
}

.stat-label {
  font-size: 14px;
  color: var(--text-secondary);
}

.content-card {
  background: var(--bg-card);
  border: 1px solid var(--border-color);
  backdrop-filter: blur(10px);
}

.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.content-tabs {
  :deep(.el-tabs__nav-wrap::after) {
    background-color: var(--border-color);
  }

  :deep(.el-tabs__item) {
    color: var(--text-secondary);
    display: flex;
    align-items: center;
    gap: 5px;

    &.is-active {
      color: var(--laser-cyan);
    }

    &:hover {
      color: var(--laser-cyan);
    }
  }

  :deep(.el-tabs__active-bar) {
    background-color: var(--laser-cyan);
    height: 2px;
  }
}

.profile-section,
.settings-section,
.notifications-section,
.privacy-section {
  padding: 20px 0;
}

.info-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
  gap: 20px;
}

.info-item {
  background: var(--bg-secondary);
  padding: 15px;
  border-radius: 8px;
  border: 1px solid var(--border-color);
  transition: all 0.3s ease;

  &:hover {
    transform: translateY(-2px);
    box-shadow: var(--glow-shadow);
  }
}

.info-label {
  font-size: 14px;
  color: var(--text-secondary);
  margin-bottom: 5px;
}

.info-value {
  font-size: 16px;
  color: var(--text-primary);
  font-weight: bold;
}

.settings-form,
.notifications-form,
.privacy-form {
  max-width: 500px;
  margin: 0 auto;
}

.avatar-select,
.theme-select {
  width: 100%;
}

.save-button {
  width: 100%;
  margin-top: 20px;
}

:deep(.el-form-item__label) {
  color: var(--text-secondary);
}

:deep(.el-input__wrapper),
:deep(.el-select__wrapper) {
  background: var(--bg-secondary);
  border: 1px solid var(--border-color);
  box-shadow: none;

  &:hover {
    border-color: var(--laser-cyan);
  }

  &.is-focus {
    border-color: var(--laser-cyan);
    box-shadow: 0 0 0 1px var(--laser-cyan);
  }
}

:deep(.el-checkbox__input.is-checked .el-checkbox__inner) {
  background-color: var(--laser-cyan);
  border-color: var(--laser-cyan);
}

:deep(.el-radio__input.is-checked .el-radio__inner) {
  background-color: var(--laser-cyan);
  border-color: var(--laser-cyan);
}
</style>
